package com.scu.controller;
import com.scu.pojo.Topic;
import com.scu.pojo.User;
import com.scu.pojo.UserSearch;
import com.scu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {

    /*
    * Author: WenjunPeng
    * Time: 06/11 2020 14:57
    */

    @Autowired
    private UserService userService;

    @RequestMapping(value="/checkLoginStatus")
    @ResponseBody
    public HashMap checkLoginStatus(HttpServletRequest request) {
        HashMap<String, Object> resultMap = new HashMap<>();
        if(request.getSession().getAttribute("u_id") != null){
            resultMap.put("code",200);
            resultMap.put("user_name",request.getSession().getAttribute("u_name"));
            resultMap.put("user_id",request.getSession().getAttribute("u_id"));
            resultMap.put("user_email",request.getSession().getAttribute("u_email"));
            resultMap.put("user_img",request.getSession().getAttribute("u_img"));
            resultMap.put("user_gender",request.getSession().getAttribute("u_gender"));
        }
        else{
            resultMap.put("code",-1);
        }
        return resultMap;
    }


    @RequestMapping(value="/checkLoginInfo")
    @ResponseBody
    public HashMap Login(HttpServletRequest request){
        HashMap<String, Object> resultMap = new HashMap<>();
        String email = request.getParameter("user_email");
        String pwd = request.getParameter("user_pwd");
        List<User> userList = userService.checkLoginInfo(email, pwd);
        System.out.println("login");
        if (userList.size() == 1) {
            request.getSession().setAttribute("u_id", userList.get(0).getU_id());
            request.getSession().setAttribute("u_name", userList.get(0).getU_name());
            request.getSession().setAttribute("u_email", userList.get(0).getU_email());
            request.getSession().setAttribute("u_img", userList.get(0).getU_img());
            request.getSession().setAttribute("u_gender", userList.get(0).getU_gender());
            int status = 1;
            userService.modifyOnlineInfo(userList.get(0).getU_id(),status);
            System.out.println("Method:Login, user id:" + userList.get(0).getU_id());
            resultMap.put("code", 200);
//            resultMap.put("data",userList);
        }
        else {
            resultMap.put("code", -1);
        }
        return resultMap;
    }

    @RequestMapping(value="/logout")
    @ResponseBody
    public HashMap Logout(HttpServletRequest request){
        int id = 0;
        int status = 0;
        if(request.getSession().getAttribute("u_id") != null) {
            id = (int)request.getSession().getAttribute("u_id");
            request.getSession().removeAttribute("u_id");
        }

        if(request.getSession().getAttribute("u_name") != null) {
            request.getSession().removeAttribute("u_name");
        }

        if(request.getSession().getAttribute("u_gender") != null) {
            request.getSession().removeAttribute("u_gender");
        }

        System.out.println("Method:Logout, user id:"+id);
        int code = userService.modifyOnlineInfo(id,status);
        HashMap<String,Object> resultMap = new HashMap<>();
        if(code > 0) {
            resultMap.put("code", 200);
        }
        else{
            resultMap.put("code", -1);
        }
        return resultMap;
    }

    @RequestMapping(value="/addRegisterInfo")
    @ResponseBody
    public HashMap Register(HttpServletRequest request){
        String name = request.getParameter("re_name");
        String pwd = request.getParameter("re_pwd");
        String email = request.getParameter("email");
//        String img = request.getParameter("user_img");
        int gender = 0;


        User insertedUser = new User();
        insertedUser.setU_name(name);
        insertedUser.setU_pwd(pwd);
        insertedUser.setU_email(email);
        insertedUser.setU_img("");
        insertedUser.setU_gender(gender);
        HashMap<String,Object> resultMap = new HashMap<>();
        if(name == null || pwd == null || email == null){
            resultMap.put("code",-1);
            return resultMap;
        }
        else{
            int status = 1;
            userService.addRegisterInfo(insertedUser);
            request.getSession().setAttribute("u_id",insertedUser.getU_id());
            request.getSession().setAttribute("u_name",name);
            request.getSession().setAttribute("u_gender",gender);
            userService.modifyOnlineInfo(insertedUser.getU_id(),status);
            resultMap.put("code",200);
            System.out.println("Method:Register, user id:"+insertedUser.getU_id());

            return resultMap;
        }


    }

//    @RequestMapping(value="/getBackPassword")
//    @ResponseBody
//    public HashMap getBackPassword(HttpServletRequest request){
//        HashMap<String,Object> resultMap = new HashMap<>();
//        return resultMap;
//    }

    @RequestMapping(value="/checkFriends")
    @ResponseBody
    public HashMap checkFriends(HttpServletRequest request){
        HashMap<String,Object> resultMap = new HashMap<>();
        if(request.getSession().getAttribute("u_id") != null){
            int id = (int)request.getSession().getAttribute("u_id");
            List<User> userList = userService.checkFriends(id);
            resultMap.put("code",200);
            resultMap.put("friends",userList);
            System.out.println("Method:checkFriends, user id:"+id);
            return resultMap;
        }
        else{
            resultMap.put("code",-1);
            return resultMap;
        }
    }

    @RequestMapping(value="/checkFriendsByPage")
    @ResponseBody
    public HashMap checkFriendsByPage(HttpServletRequest request,UserSearch info){
        HashMap<String,Object> resultMap = new HashMap<>();
        if(request.getSession().getAttribute("u_id") != null){
            int id = (int)request.getSession().getAttribute("u_id");
            info.setId(id);
            List<User> userList = userService.checkFriendsByPage(info);
            int count = userService.getFriendsCount(info);
            resultMap.put("code",0);
            resultMap.put("count",count);
            resultMap.put("friends",userList);
            System.out.println("Method:checkFriends, user id:"+id);
            return resultMap;
        }
        else{
            resultMap.put("code",-1);
            return resultMap;
        }
    }


    @RequestMapping(value="/addFriend")
    @ResponseBody
    public HashMap addFriend(HttpServletRequest request){

        HashMap<String,Object> resultMap = new HashMap<>();
        int code = 0;
        if(request.getParameter("target_id") != null && request.getSession().getAttribute("u_id") != null){
            int target_id = Integer.parseInt(request.getParameter("target_id"));
            int source_id = (int)request.getSession().getAttribute("u_id");
            code = userService.addFriend(source_id,target_id);
            System.out.println("Method:addFriend, user id:"+source_id);
        }

        if(code > 0) {
            resultMap.put("code", 200);
            resultMap.put("msg", "关注成功");
        }
        else{
            resultMap.put("code", -1);
        }
        return resultMap;
    }

    @RequestMapping(value="/deleteFriend")
    @ResponseBody
    public HashMap deleteFriend(HttpServletRequest request) {
        HashMap<String,Object> resultMap = new HashMap<>();
//        System.out.println("delete");
        int code = 0;
        if(request.getSession().getAttribute("u_id") == null){
            resultMap.put("code", -1);
//            System.out.println("delete out if");
        }
        else{
            System.out.println("delete in if " + request.getParameter("target_id"));
            int target_id = Integer.parseInt(request.getParameter("target_id"));
//            System.out.println("delete get value");
            int source_id = (int)request.getSession().getAttribute("u_id");
//            System.out.println("delete get value");
            code = userService.deleteFriend(source_id,target_id);
            System.out.println("Method:deleteFriend, user id:"+source_id);

            if(code > 0){
                resultMap.put("code", 200);
                resultMap.put("msg", "删除成功");
            }
            else{
                resultMap.put("code", 0);
            }
        }
        return resultMap;
    }


    @RequestMapping(value="/modifyLoginInfo")
    @ResponseBody
    public HashMap modifyLoginInfo(HttpServletRequest request) {
        HashMap<String,Object> resultMap = new HashMap<>();
        if(request.getSession().getAttribute("u_id") == null){
            resultMap.put("code",-1);
            return resultMap;
        }
        else{
            User user = new User();
            user.setU_id((int)request.getSession().getAttribute("u_id"));
            user.setU_email(request.getParameter("modified_email"));
            user.setU_pwd(request.getParameter("modified_pwd"));

            System.out.println("Method:modifyLoginInfo, user_id:" + user.getU_id() + ", modified_email:"+user.getU_email()+", modified_pwd:"+user.getU_pwd());
            int code = userService.modifyLoginInfo(user);

            if(code > 0) {
                resultMap.put("code", 200);
            }
            else{
                resultMap.put("code", 0);
            }
            return resultMap;
        }

    }

    @RequestMapping(value="/modifyBasicInfo")
    @ResponseBody
    public HashMap modifyBasicInfo(HttpServletRequest request) {
        HashMap<String,Object> resultMap = new HashMap<>();
        if(request.getSession().getAttribute("u_id") == null){
            resultMap.put("code",-1);
            return resultMap;
        }
        else{
            User user = new User();
            user.setU_id((int)request.getSession().getAttribute("u_id"));
            user.setU_gender(Integer.parseInt(request.getParameter("modified_gender")));
            user.setU_img(request.getParameter("modified_img"));
            user.setU_name(request.getParameter("modified_name"));
            user.setU_email(request.getParameter("modified_email"));
            request.getSession().setAttribute("u_name",user.getU_name());
            request.getSession().setAttribute("u_gender",user.getU_gender());
            request.getSession().setAttribute("u_email",user.getU_email());
            System.out.println("Method:modifyLoginInfo, user_id:" + user.getU_id() + ", modified_gender:"+user.getU_gender()+", modified_img:"+user.getU_img());
            int code = userService.modifyLoginInfo(user);
            if(code > 0) {
                resultMap.put("code", 200);
            }
            else{
                resultMap.put("code", 0);
            }
            return resultMap;
        }

    }

    @PostMapping(value="/modifyUserImg")
    @ResponseBody
    public Map postTopic(@RequestParam(value = "photo", required = false) MultipartFile photo, User user, HttpSession session, HttpServletRequest request){
        HashMap<String,Object> map = new HashMap<>();
        System.out.println("userImg");
        System.out.println(user);
        String saveLocal = "";
        try {
            // 当前文件大小
            long currentFileSize = photo.getSize();
            // 上传源文件允许的最大值
            long fileLength = 5242880;
            String realPath = request.getSession().getServletContext().getRealPath("/");
            System.out.println("realPath " + realPath);
            if (currentFileSize <= fileLength) {
                System.out.println("文件类型：" + photo.getClass());
                System.out.println("文件：" + photo.getOriginalFilename());
                userService.approvalFile(photo,realPath);
                String picturePath =  "/static/img/userImg/" + photo.getOriginalFilename();
                user.setU_img(picturePath);
                user.setU_email(null);
                user.setU_name(null);
                user.setU_id((int)request.getSession().getAttribute("u_id"));
                user.setU_pwd(null);
                user.setU_forbidden_date(null);
                user.setU_register_date(null);
                request.getSession().setAttribute("u_img", picturePath);
                // 将article保存到数据库
                int code = userService.modifyLoginInfo(user);
                map.put("code",200);
                map.put("msg","文章上传成功");
                return map;
            } else {
                map.put("msg","上传文件过大");
                map.put("code",-1);
                return map;
            }
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg","更换头像失败");
            map.put("code",-2);
            return map;
        }
    }

    @RequestMapping(value="/checkIsFollowedByMe")
    @ResponseBody
    public HashMap checkIsFollowedByMe(HttpServletRequest request) {
        HashMap<String,Object> resultMap = new HashMap<>();
        if(request.getSession().getAttribute("u_id") == null){
            resultMap.put("code",-1);
        }
        else {
            int u_id = (int)request.getSession().getAttribute("u_id");
            int target_id = Integer.parseInt(request.getParameter("target_id"));
            int code = userService.checkIsFollowedByMe(u_id,target_id);
            resultMap.put("code",code);
        }

        return resultMap;

    }


    @RequestMapping(value="/checkMyFollows")
    @ResponseBody
    public HashMap checkMyFollows(HttpServletRequest request) {
        HashMap<String,Object> resultMap = new HashMap<>();
        if(request.getSession().getAttribute("u_id") == null){
            resultMap.put("code",-1);
        }
        else {
            int u_id = (int)request.getSession().getAttribute("u_id");
            List<User> userList = userService.checkMyFollows(u_id);
            resultMap.put("code",200);
            resultMap.put("myFollows",userList);
        }

        return resultMap;

    }
}
